package immoc_collection.map.demo;

import java.util.*;

public class SetTest {

    public List<Course> courseToSelect;

    public SetTest() {
        courseToSelect = new ArrayList<Course>();
    }

    public void testAdd() {
        // 创建一个课程对象
        Course cr1 = new Course("1", "数据结构");
        // 添加入课程List中
        courseToSelect.add(cr1);
        Course cr2 = new Course("2", "C语言");
        courseToSelect.add(0, cr2);
        Course[] courses = {new Course("3", "离散数学"), new Course("4", "汇编语言")};
        courseToSelect.addAll(Arrays.asList(courses));
    }

    public void testForeach() {
        for (Course temp : courseToSelect) {
            System.out.println("课程，ID: " + temp.id + " ：" + temp.name);
        }
    }

    public void testSetContain(Student student) {
        Course c = new Course("1", "数据结构");
        System.out.println("student : " + student.name + " 包含了课程 " + c.name + "? " + student.courses.contains(c));;
    }

    public static void main(String[] args) {
        SetTest st = new SetTest();
        st.testAdd();
        st.testForeach();

        Student s = new Student("1", "小明");
        System.out.println("欢迎小明同学来选课");

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入课程ID：");
            String courseId = scanner.next();
            for (Course cr: st.courseToSelect) {
                if (cr.id.equals(courseId)) {
                    System.out.println("所选课程 ID： " + cr.id + "" + cr.name);
                    s.courses.add(cr);
                    s.courses.add(cr); // 验证是否可以重复添加
                    // 无法添加重复元素
                }
            }
        }

        System.out.println("学生 " + s.id + ":" + s.name + "所选课程，如下： ");
        // 输出学生所选的课程 （由于Set是无序 ，所以每次循环出来的顺序不一样）
        for (Course cr : s.courses) {
            System.out.println(" ID： " + cr.id + ":" + cr.name);
        }


        st.testSetContain(s);
    }
}
